#include <stdio.h>

// 排序算法

void printArr(int [], int);
void bubble(int [], int);
void select(int [], int);

int main(int argc, char *argv[]) {
	int arr[9] = {12, 44, 25, 18, 37, 1, 52, 67, 86};
	int length = sizeof(arr) / sizeof(int);
	printArr(arr, length);
	
	// bubble(arr, length);
	
	select(arr, length);
	
	printArr(arr, length);
	return 0;
}

// 交换值
void swap(int *pointer1, int *pointer2) {
	int temp = 0;
	temp = *pointer1;
	*pointer1 = *pointer2;
	*pointer2 = temp;
}

// 打印数组
void printArr(int arr[], int length) {
	// error: 'for' loop initial declarations are only allowed in C99 mode
	// note: use option -std=c99 or -std=gnu99 to compile your code
	// for (int i = 0; i < length; i++) {
	
	int i = 0;
	for (; i < length; i++) {
		printf("%-8d", arr[i]);// 输出 8 位，并且左对齐
	}
	printf("\n");
}

// 冒泡排序
void bubble(int arr[], int length) {
	int i = 0;
	int j = 0;
	for (; i < length; i++) {
		for (j = 1; j < length - i; j++) {
			if (arr[j] < arr[j - 1]) {
				swap(&arr[j], &arr[j - 1]);
			}
		}
	}
}

// 选择排序
void select(int arr[], int length) {
	int i = 0;
	int j = 0;
	for (; i < length - 1; i++) {
		for (j = i + 1; j < length; j++) {
			if (arr[i] > arr[j]) {
				swap(&arr[i], &arr[j]);
			}
		}
	}
}
